home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 637 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: STL pop_back()
  5. Date: 05 Mar 1996 09:50:19 PST
  6. Organization: Fakultdt f|r Mathematik und Informatik
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4hhsta$2gk@news.BelWue.DE>
  9. References: <4h4hmr$41o@news.rwth-aachen.de> <4h78mr$alj@engnews1.Eng.Sun.COM> <313BD2CA.7DBD@tiac.net>
  10. Reply-To: dietmar.kuehl@uni-konstanz.de
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 5 Mar 1996 17:16:26 GMT
  13. X-Newsreader: TIN [version 1.2 PL2]
  14. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  15.     iQBVAwUBMTx/Gky4NqrwXLNJAQHq0wIAillW5NqNOJSOCNnkPo+N9YdwezUFnlZj
  16.     lid7XFh8fxL8hAi2Vf0snT0DFEowA4Kt59acbfyhPmZkh3L0S5CeUQ==
  17.     =N83Y
  18. Originator: austern@isolde.mti.sgi.com
  19.  
  20. Hi,
  21.  
  22. Hayden Schultz (haydens@tiac.net) wrote:
  23. : Why don't the pop functions return a copy of the object before they
  24. : blow it away?
  25.  
  26. These containers are simply not intended to be used as stack (or queue)
  27. directly. Instead, you can use 'stack<Container<T> >' or
  28. 'queue<Container<T> >' to use the class 'Container' as stack (or
  29. queue). These adaptors can be implemented in terms of 'push_back()' and
  30. 'pop_back()'. However, 'stack<...>::pop()' also returns 'void'. The
  31. reason is simple: If you need the value, you can get it with a sequence
  32. of 'top()' and 'pop()' operations. Requiring that 'pop()' returns an
  33. object would make it necessary to create this object which in turn can
  34. be expensive. This way it is defined everything is possible
  35. efficiently.
  36.  
  37. : Another possibility, I suppose, would be an auto_ptr<T>
  38. : so that the object is deleted if it's not used. An auto_ptr<T> is
  39. : about as efficient as deleting it in the pop function, it just changes
  40. : the time it's deleted (which isn't guaranteed anywhere I've read
  41. : anyway).
  42.  
  43. Note, that STL containers in general store objects, not pointers.
  44. Returning an 'auto_ptr<T>' would require that pointers are stored. This
  45. is inefficient e.g. if you want to store 'int's.
  46.  
  47. : Why call the methods push and pop if it doesn't act like a stack?
  48.  
  49. These functions are e.g. used to implement a stack adapter which acts
  50. like a stack or a queue.
  51. --
  52. dietmar.kuehl@uni-konstanz.de
  53. http://www.informatik.uni-konstanz.de/~kuehl
  54. I am a realistic optimist - that's why I appear to be slightly pessimistic
  55. ---
  56. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  57.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  58.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  59.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  60.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  61. ]
  62.